<?php
class Crud extends CI_Controller {
	function __construct() {
		// Call the Controller constructor
		parent::__construct();
	}
	
	function index() {
		$info = $this->session->userdata('logged');

		if($info == true) {
			$data['title']='Dashboard';
			$data['main']='generic.php';
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
	
	function deleteAll() {
		$info = $this->session->userdata('logged');
		
		$this->load->model('showtime_model');
		$this->load->model('ticket_model');
		
		$this->db->query('delete from ticket');
		$this->db->query('update showtime set available = 3');
		
		if($info == true) {
			$data['title']='Dashboard';
			$data['main']='generic.php';
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
	
	function showAll() {
		$info = $this->session->userdata('logged');
		$this->load->model('ticket_model');
		
		$tickets = $this->ticket_model->get_tickets();
		
		if($info == true) {
			$data['title'] = 'Dashboard';
			$data['main'] = 'list.php';
			$data['tickets'] = $tickets;
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
	
	function insert() {
		$info = $this->session->userdata('logged');
		if($info == true) {
			$data['title'] = 'Dashboard';
			$data['main'] = 'query.php';
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
	
	function runQuery() {
		$info = $this->session->userdata('logged');
		$results = $this->db->query($this->input->get_post('query'));
		
		if($results == false) {
			$data['errorMsg'] = $this->db->_error_message();
    		$data['errorNo'] = $this->db->_error_number();
		}
    	
		if($info == true) {
	    	$data['title'] = 'Dashboard';
			$data['main'] = 'query.php';
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
	
	function logout() {
		$state = false;
		$this->session->unset_userdata('logged');
		header("Location: ../../index.php/admin");
   		
	}
	
	function login() {
		if(isset($_POST['Submit']))
			$state = $_POST['Submit'];
		if($state == "log") {
			$this->session->set_userdata('logged', TRUE);
			$info = $this->session->userdata('logged');
			$data['main'] = 'generic.php';
			$this->load->view('dashboard', $data);
		} else {
			header("Location: ../../index.php/admin");
		}
	}
}